import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {path:'/',redirect:"/login"}
 ,{path:"/login",component:()=>import("components/main/login")}
 
 ,{
    path:"/home"  
   ,component:()=>import("components/main/home")
   ,redirect:"/welcome"
   ,children:[
     {path:"/welcome",component:()=>import("components/main/welcome")}
    ,{path:"/users",component:()=>import("components/users/User")}
    ,{path:"/rights",component:()=>import("components/power/rights")}
    ,{path:"/roles",component:()=>import("components/power/roles")}
    ,{path:"/categories",component:()=>import("components/goods/category")}
    ,{path:"/params",component:()=>import("components/goods/params")}
    ,{path:"/goods",component:()=>import("components/goods/list")}
    ,{path:"/goods/info",component:()=>import("components/goods/info")}
    ,{path:"/orders",component:()=>import("components/order/order")}
    ,{path:"/reports",component:()=>import("components/report/report")}
   ]
 }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next)=>{
  //获取通行证
  const token= window.sessionStorage.getItem('token')
  //进入login不需要通行证
  if(to.path.includes('login')){
    //从哪来的回哪去
    if(token && token.length>150){
      let path= from.path
      if(path==='/')path="/home";
      return next(path)
    }
    return next() 
  }
  //没有通行证则强制进入登录页
  if(!(token && token.length>150)) return next("/login");
  //否则放行
  next()
  
})
export default router
